package com.sh.data.engine.domain.datadev.offline.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.data.engine.domain.datadev.offline.service.DatadevOfflineSyncService;
import com.sh.data.engine.domain.datadev.offline.service.DatadevSyncFieldMappingService;
import com.sh.data.engine.domain.datadev.offline.service.DatadevSyncPartitionService;
import com.sh.data.engine.domain.integration.offline.model.domain.OfflineSyncDomain;
import com.sh.data.engine.domain.integration.offline.model.domain.OfflineSyncDomain.FieldMapping;
import com.sh.data.engine.domain.integration.offline.model.domain.OfflineSyncDomain.PartitionField;
import com.sh.data.engine.repository.dao.datadev.DatadevOfflineSyncMapper;
import com.sh.data.engine.repository.dao.datadev.entity.DatadevOfflineSyncEntity;
import com.sh.data.engine.repository.dao.datadev.entity.DatatdevSyncFieldMappingEntity;
import com.sh.data.engine.repository.dao.datadev.entity.DatatdevSyncPartitionEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DatadevOfflineSyncServiceImpl
    extends ServiceImpl<DatadevOfflineSyncMapper, DatadevOfflineSyncEntity>
    implements DatadevOfflineSyncService {

    @Autowired
    private DatadevSyncFieldMappingService datadevSyncFieldMappingService;

    @Autowired
    private DatadevSyncPartitionService datadevSyncPartitionService;

    @Override
    public DatadevOfflineSyncEntity getDatadevOfflineSyncNodeId(Long nodeId) {
        LambdaQueryWrapper<DatadevOfflineSyncEntity> eq =
            new LambdaQueryWrapper<DatadevOfflineSyncEntity>()
                .eq(DatadevOfflineSyncEntity::getNodeId, nodeId);
        DatadevOfflineSyncEntity one = getOne(eq, false);
        return one;
    }

    @Override
    public OfflineSyncDomain getSyncTaskByNodeId(Long nodeId) {
        DatadevOfflineSyncEntity offlineDevSyncTask = this.getDatadevOfflineSyncNodeId(nodeId);
        OfflineSyncDomain offlineSyncDomain =
            Convert.convert(OfflineSyncDomain.class, offlineDevSyncTask);
        List<DatatdevSyncFieldMappingEntity> syncFieldMappingByNodeId =
            datadevSyncFieldMappingService.getSyncFieldMappingByNodeId(nodeId);
        List<DatatdevSyncPartitionEntity> partitionEntityList =
            datadevSyncPartitionService.getSyncFieldMappingByNodeId(nodeId);

        List<PartitionField> partitionFields =
            Convert.toList(PartitionField.class, partitionEntityList);
        List<FieldMapping> fieldMappings = Convert.toList(FieldMapping.class, syncFieldMappingByNodeId);
        offlineSyncDomain.setFieldMappings(fieldMappings);
        offlineSyncDomain.setParitions(partitionFields);
        return offlineSyncDomain;
    }

    @Override
    public List<DatadevOfflineSyncEntity> listDatadevSyncTask(Long dsId) {

        LambdaQueryWrapper<DatadevOfflineSyncEntity> eq =
            new LambdaQueryWrapper<DatadevOfflineSyncEntity>()
                .eq(DatadevOfflineSyncEntity::getSourceDsId, dsId)
                .or()
                .eq(DatadevOfflineSyncEntity::getTargetDsId, dsId);

        List<DatadevOfflineSyncEntity> list = list(eq);
        return list;
    }
}
